package com.niiwoo.civet.trade.service.dubbo.loan.core;

import com.alibaba.fastjson.JSON;
import com.niiwoo.asset.cardniu.dto.mq.CardNiuExternalMQConstants;
import com.niiwoo.asset.cardniu.dto.request.LoanStatusDTO;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.mapper.ProjectMapper;
import com.niiwoo.civet.trade.dto.request.loan.PushProjectRequestDTO;
import com.niiwoo.civet.trade.enums.TradeConfigEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:卡牛推标
 * @Author:xiaoyaocai
 * @Date:2018/05/08
 */
@Slf4j
@Service
public class CardNiuSpeedLoanServiceImpl extends AbstractSpeedLoan {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    protected void sendInstationInfo(Project project){
        log.info("卡牛-推标-不需要发站内信息：projectId={}",project.getProjectId());
    }

    @Override
    protected void pushProjectSuccess(PushProjectRequestDTO pushProjectRequestDTO, Project project) {
        super.pushProjectSuccess(pushProjectRequestDTO, project);
        this.sendMQForNotify(project);
    }

    /**
     * 获取配置Key
     * @return TradeConfigEnum
     */
    @Override
    protected TradeConfigEnum getTradeConfigEnum(){
        return TradeConfigEnum.CARDNIU_INVESTDEADLINE ;
    }

    private void sendMQForNotify(Project project){
        try{
            /*Project projectMQ=projectMapper.selectByPrimaryKey(project.getProjectId());
            if(projectMQ.getStatus().equals(ProjectStatusEnum.COLLECTING.getStatus().byteValue()) &&
                    projectMQ.getSubStatus().equals(ProjectSubStatusEnum.COLLECTING_INIT.getSubStatus())){
            }else{
                log.info("卡牛-推标，失败，MQ-notify 取消发送,{}",projectMQ.getProjectId());
            }*/
            log.info("卡牛-推标，MQ发送标的信息：{}", JSON.toJSONString(project));
            LoanStatusDTO loanStatusDTO = new LoanStatusDTO();
            loanStatusDTO.setProjectId(project.getProjectId());
            loanStatusDTO.setStatus(project.getStatus());
            loanStatusDTO.setSubStatus(project.getSubStatus());
            loanStatusDTO.setType(project.getType());
            rabbitTemplate.convertAndSend(CardNiuExternalMQConstants.Exchange.LOAN_STATUS_EXCHANGE,
                    CardNiuExternalMQConstants.getRoutingKey(CardNiuExternalMQConstants.Exchange.LOAN_STATUS_EXCHANGE, CardNiuExternalMQConstants.Queue.LOAN_STATUS_QUEUE), loanStatusDTO);
            log.info("卡牛-推标，MQ发送标的信息：projectId={},status={},subStatus={}", project.getProjectId(),project.getStatus(),project.getSubStatus());

        }catch(Exception e){
            log.error("卡牛-推标，MQ发送标的信息失败：projectId={},status={},subStatus={}", project.getProjectId(),project.getStatus(),project.getSubStatus());
        }
    }
}
